import { cloudStorage } from "common/jscloud";
import { actionEvents } from 'common/action'
import _ from 'lodash'
const {lib, str2Runing} = ao2

/**
 * 动态构建bootstrap的columns 
 * @param {Array} columns bootstrap table所需的column原始数据
 * @param {Function} getTable 获取表格实例
 * @returns {Array} bootstrap table 所需的column数据
 */
export function bootstrapTableColumns(options, getTable) {
  const mycolumns = []
  const columnsName = options.columnsName
  const collectionName = options.collectionName
  const columns = options.columns
  const events = options.events||{}
  columns.forEach(col=>{
    for (const [key, _value] of Object.entries(col)) {
      let value = Object.assign({}, _value)

      if (key !== 'methods') {  // methods用于cell组件中，表单响应事件
        if (value.events) {
          if (value.events === true) {
            value.events = actionEvents(null, getTable, columnsName, columns, collectionName)
          } else {
            if (typeof value.events === 'object') {
              const events = actionEvents(null, getTable, columnsName, columns, collectionName)
              value.events = Object.assign(events, value.events)
            }
            if (typeof value.events === 'string') {
              value.events = actionEvents(value.events, getTable, columnsName, columns, collectionName) || {}
            }
          }
        }

        if (value.formatter) {
          let formatter = ''
          if (typeof value.formatter !== 'function') {
            if (typeof value.formatter === 'string') {
              formatter = [value.formatter]
            }
            if (Array.isArray(value.formatter)) {
              formatter = value.formatter.join('')
            }
            value.formatter = function(value, row, index){
              const compiled = _.template(formatter)
              return compiled({value, row, index})
            }
          }
        }

        const tmp = Object.assign({}, {field: key}, value);
        tmp.events = Object.assign({}, tmp.events, events)
        delete tmp.input
        mycolumns.push(tmp)
      }
    }
  })
  return mycolumns
}


// 获取bootstrap table的表头定义
export async function getColumnsFromManager(target) {
  const gcc = {}
  const globalCloudCollection = window.globalCloudCollection  //生成路由菜单时定义
  const collectionName = target.collectionName


  // 从全局配置拿取该集合的配置
  if (globalCloudCollection) {
    let fildsContentStr = globalCloudCollection[collectionName].filds
    if (typeof fildsContentStr === 'string') {
      return str2Runing(fildsContentStr)
    }
  }

  
  const manager = cloudStorage('CLOUD-TABLE-MANAGER')
  const list = await manager.doc.list()
  let fildsContentStr = ''
  for (let ii=0; ii<list.data.length; ii++) {
    const col = list.data[ii]
    const data = col.data
    const tableName = data.tableName
    gcc[tableName] = data
    if (tableName === collectionName) {
      fildsContentStr = data.filds;
    }
  }
  window.globalCloudCollection = gcc
  if (fildsContentStr) {
    return str2Runing(fildsContentStr)
  }
}